package com.shopping.dao.impl;

import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import com.shopping.bean.User;
import com.shopping.dao.UserDao;
import com.shopping.superDao.SuperDao;
import com.shopping.superDao.impl.SuperDaoImpl;
import com.shopping.util.DBUtil;

public class UserDaoImpl implements UserDao {

	JdbcTemplate jt = new JdbcTemplate(DBUtil.getDataSource());
	SuperDao sd = new SuperDaoImpl();
	
	@Override
	public int insertUser(User user) {
		String sql = "INSERT INTO users(userName,passWord,phone,register,userSex) VALUES(?,?,?,?,?)";
		return jt.update(sql,user.getUserName(),user.getPassWord(),user.getPhone(),user.getRegister(),user.getUserSex());
	}

	@Override
	public User selectUser(String username, String password) {
		String sql = "SELECT * FROM users WHERE userName = ? AND passWord = ?";
		List<User> list = jt.query(sql, new BeanPropertyRowMapper<User>(User.class),username,password);
		if(list.size() == 0) {
			return null;
		}else {
			return list.get(0);
		}
	}

	@Override
	public User selectUser(String username) {
		String sql = "SELECT * FROM users WHERE userName = ?";
		List<User> list = jt.query(sql, new BeanPropertyRowMapper<User>(User.class),username);
		if(list.size() == 0) {
			return null;
		}else {
			return list.get(0);
		}
	}

	@Override
	public int selectUserId(String username, String password) {
		String sql = "SELECT userId FROM users WHERE userName = ? AND passWord = ?";
		return jt.queryForObject(sql, Integer.class,username,password);
	}

	@Override
	public Map<String, Object> selectUserByName(String userName) {
		return sd.selectByName(userName, "users", "userName");
	}

	@Override
	public int updateUserMassage(User user) {
		String sql = "UPDATE users SET userName = ?,passWord = ?,address = ?,phone = ?,userImage = ?,userSex = ? WHERE userId = ?";
		return jt.update(sql,user.getUserName(),user.getPassWord(),user.getAddress(),user.getPhone(),user.getUserImage(),user.getUserSex(),user.getUserId());
	}

	@Override
	public List<User> selectUser1(String userName, String passWord) {
		String sql = "select * from users where userName = ? and passWord = ?";
		return jt.query(sql, new BeanPropertyRowMapper<User>(User.class),userName,passWord);
	}

	
}
